/********************************************************************
 * Author: ApOgEE
 * http://coderstalk.blogspot.com
 ********************************************************************/

var relURLs = new Array();
var thumbURL = new Array();
var relTitles = new Array();
var relTitlesNum = 0;

function related_results_labels_thumbs(json) {
	
	for (var i = 0; i < json.feed.entry.length; i++) {
		var entry = json.feed.entry[i];
		relTitles[relTitlesNum] = entry.title.$t;
		try {
			thumbURL[relTitlesNum]=entry.media$thumbnail.url;
		} catch (error) {
			s=entry.content.$t;a=s.indexOf("<img");
			b=s.indexOf("src=\"",a);
			c=s.indexOf("\"",b+5);
			d=s.substr(b+5,c-b-5);
			if((a!=-1)&&(b!=-1)&&(c!=-1)&&(d!="")){
				thumbURL[relTitlesNum]=d;
			} else {
				thumbURL[relTitlesNum]='http://lh4.ggpht.com/_QmenDvyMjlk/S3NokfajzPI/AAAAAAAAB-U/OXt0uR2Jux8/s800/nothumb.jpg';
			}
		}
		
		if(relTitles[relTitlesNum].length>35) {
			relTitles[relTitlesNum]=relTitles[relTitlesNum].substring(0, 35)+"...";
		}
		
		for (var k = 0; k < entry.link.length; k++) {
			if (entry.link[k].rel == 'alternate') {
				relURLs[relTitlesNum] = entry.link[k].href;
				relTitlesNum++;
			}
		}
	}
}

function removeRelatedDuplicates_thumbs() {
	var tmp = new Array(0);
	var tmp2 = new Array(0);
	var tmp3 = new Array(0);
	for(var i = 0; i < relURLs.length; i++) {
		if(!contains_thumbs(tmp, relURLs[i])) {
			tmp.length += 1;
			tmp[tmp.length - 1] = relURLs[i];
			tmp2.length += 1;
			tmp3.length += 1;
			tmp2[tmp2.length - 1] = relTitles[i];
			tmp3[tmp3.length - 1] = thumbURL[i];
		}
	}

	relTitles = tmp2;
	relURLs = tmp;
	thumbURL=tmp3;
}

function contains_thumbs(a, e) {
	for(var j = 0; j < a.length; j++) {
		if (a[j]==e) return true;
	}
	return false;
}

function printRelatedLabels_thumbs() {
	for(var i = 0; i < relURLs.length; i++) {
		if((relURLs[i]==currentposturl)||(!(relTitles[i]))) {
			relURLs.splice(i,1);
			relTitles.splice(i,1);
			thumbURL.splice(i,1);
		}
	}

	var r = Math.floor((relTitles.length - 1) * Math.random());
	var i = 0;

	if(relTitles.length>0) document.write('<h2>'+relatedpoststitle+'</h2>');
	document.write('<div style="clear: both;"/>');
	while (i < relTitles.length && i < 20 && i<maxresults) {
		document.write('<a style="text-decoration:none;padding:5px;float:left;');
		if(i!=0) {
			document.write('border-left:solid 0.5px #d4eaf2;"');
		} else {
			document.write('"');
		}
		document.write(' href="' + relURLs[r] + '"><img style="width:72px;height:72px;border:0px;" src="'+thumbURL[r]+'"/><br/><div style="width:72px;padding-left:3px;height:65px;border: 0pt none ; margin: 3px 0pt 0pt; padding: 0pt; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: normal; font-size-adjust: none; font-stretch: normal;">'+relTitles[r]+'</div></a>');

		if (r < relTitles.length - 1) {
			r++;
		} else {
			r = 0;
		}
		i++;
	}

	document.write('</div>');
}
